package com.jspeed.hibernate.dialogservices.client;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jspeed.hibernate.dialogservices.Devices;
import com.jspeed.hibernate.dialogservices.Users;
import com.jspeed.hibernate.dialogservices.util.HibernateUtil;

public class Client {

	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Devices devices = new Devices("hh", "kk");
			// Set<Devices> devices = new HashSet<Devices>();
			// devices.add(new Devices("nokia", "wm7"));
			// Users user = new Users("michael", devices);
			// session.save(user);
			// session.save(devices);

			List result = session.createQuery("from Users").list();

			Query query = session
					.createQuery("delete from Devices where deviceId = 26");
			query.executeUpdate();
			
			for (Iterator iter = result.iterator(); iter.hasNext();) {
				Users users1 = (Users) iter.next();
				System.out.println("id is :" + users1.getUserId() + "  "
						+ "Name is :" + users1.getUserName());
			}

			// sends the data into the DB
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}
